package com.wsc.wisdomchinaqinghe.activitity.fourmodule;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps2d.model.LatLng;
import com.bumptech.glide.Glide;
import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.OpenExternalMapAppUtils;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.application.MyApplication;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.OlderDetail;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;

/**
 * Created by Administrator on 2017/3/6 0006.
 */

public class HelpOldManDetailActivity extends BaseActivity implements View.OnClickListener, ContributorView {
    private ImageView regist_back;
    private TextView tv_title;
    private Context context;
    private ContributorPresenter contributorPresenter;
    private WebView webView;
    private String CommitteeInfoDetailFlag = "CommitteeInfoDetailFlag";
//    private MapView mMapView;
    private String nameFlag;
//    private AMap aMap;
    private TextView tv_address;
    private MyToast myToast;
    private OlderDetail newsDetailBean;
    private MyApplication myApplication;
    private ImageView iv_head;
    private String imagesrc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.helpoldmandetail_layout);
        initView();
        context = this;
        myToast=new MyToast(context);
        myApplication=(MyApplication) getApplication();
        contributorPresenter = new ContributorPresenter(this, this);
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String name = intent.getStringExtra("name");
        nameFlag = intent.getStringExtra("nameFlag");
        imagesrc=intent.getStringExtra("imagesrc");
        tv_title.setText(name);
//        mMapView.onCreate(savedInstanceState);
//        aMap = mMapView.getMap();
//        UiSettings uiSettings = aMap.getUiSettings();
//        uiSettings.setZoomControlsEnabled(false);
//        uiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_RIGHT);

        if (nameFlag.equals("助老服务")) {
            if(imagesrc!=null)
                Glide.with(context).load(imagesrc).into(iv_head);
            contributorPresenter.getOlderOrganiationDetail(id,CommitteeInfoDetailFlag);
        } else if (nameFlag.equals("助残服务")) {
            if(imagesrc!=null)
                Glide.with(context).load(imagesrc).into(iv_head);
            contributorPresenter.getHandicapOrganizationDetail(id,CommitteeInfoDetailFlag);
        }else if(nameFlag.equals("EightModule"))
        {
            contributorPresenter.getAroundFindDetail(id,CommitteeInfoDetailFlag);
            if(imagesrc!=null)
                Glide.with(context).load(imagesrc).into(iv_head);
        }

    }

    private void initView() {
        regist_back = (ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(this);
        tv_title = (TextView) findViewById(R.id.tv_headtitle);
        webView = (WebView) findViewById(R.id.fragment_zxxq_webView);
        //获取地图控件引用
//        mMapView = (MapView) findViewById(R.id.map);
        iv_head=(ImageView) findViewById(R.id.iv_head);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_address.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.regist_back:
                finish();
                break;
            case R.id.tv_address:
                try {
                    if(newsDetailBean!=null) {
                        String lng=newsDetailBean.getLng();
                        String log=newsDetailBean.getLog();

                        if(lng!=null && !lng.isEmpty() && log!=null &&!log.isEmpty())
                        {
                            LatLng latLng = new LatLng(Double.parseDouble(lng),Double.parseDouble(log));
                            LatLng starLng=new LatLng(myApplication.getLatitude(),myApplication.getLongitude());
                            OpenExternalMapAppUtils.openMapDirection(this, starLng.longitude+"", starLng.latitude+"", myApplication.getLocationAddress(),
                                    latLng.longitude+"", latLng.latitude+"", newsDetailBean.getAddr(), "智慧中国");
                        }


                    }
                }catch (Exception e)
                {

                }
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(CommitteeInfoDetailFlag)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String messageJson = JsonTools.objectToJsonString(object);
                    newsDetailBean = JsonTools.jsonStringToObject(messageJson, OlderDetail.class);
                    tv_address.setText(newsDetailBean.getAddr());
                    String Url = Constant.Head_string + newsDetailBean.getContent() + Constant.foot_string;//URL编码数据
                    webView.getSettings().setDefaultTextEncodingName("UTF-8");
                    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    webView.getSettings().setLoadWithOverviewMode(false);
                    webView.loadData(Url, "text/html; charset=UTF-8", null);
                    //        地理坐标：39°55'00.86"N 116°23'26.93"E
                    //wei du ,jingdu
//                    LatLng latLng = new LatLng(newsDetailBean.getLng(), newsDetailBean.getLog());
//                    Log.e("http",newsDetailBean.getLng()+":"+newsDetailBean.getLog());
////                    LatLng latLng = new LatLng(39.906901,116.397972);
//                    final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).title(newsDetailBean.getTitle()));

                }
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }

}
